<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
    <script src="scripts/axios.min.js"></script>
    <style>
        button {
            width: 70px;
            height: 50px;
        }
    </style>
</head>


<body>
    <button onclick="axiosTest()">axiosTest</button>
    <script type="text/javascript">
        async function axiosTest(event) {
            await axios({
                url: `http://localhost:8080/axiosTest/1`,
                method: "get",
            }).then(resp => console.log(resp)).catch(err => console.log(err));
        }
    </script>

    <h1>掘金个人名称修改</h1>
    <!-- 手动 -->
    <form id="csrf" class="form-example" enctype="multipart/form-data" method="post"
        action="https://juejin.cn/web/user/update/user_info">
        <div class="form-example">
            <label for="aid">Enter your aid: </label>
            <input type="text" name="aid" value="2608" />
        </div>
        <div class="form-example">
            <label for="name">Enter your name: </label>
            <input type="text" name="name" value="LiRuoXXXX" />
        </div>
        <div class="form-example">
            <button type="submit">self-submit</button>
        </div>
    </form>

    <!-- 自动, 为了观察设置为点击按钮才提交-->
    <form id="csrf" class="form-example">
        <div class="form-example">
            <label for="aid">Enter your aid: </label>
            <input type="text" name="aid" value="2608" />
        </div>
        <div class="form-example">
            <label for="name">Enter your name: </label>
            <input type="text" name="name" value="LiRuoXXXX" />
        </div>
        <div class="form-example">
            <button onclick="submitHandler(event)">auto-submit-but-need-to-change-something</button>
        </div>
    </form>
    <script type="text/javascript">
        function submitHandler(event) {
            console.log('aaa');
            event.preventDefault();
            console.log(event);
            // const inputs = document.querySelectorAll('input');
            // inputs.forEach(i => {
            //     console.log(i.value);
            // })
            const form = document.getElementById("csrf");
            const formData = new FormData(form);
            console.log(formData);
            // form.submit();

            (async function axiosTest() {
                await axios({
                    url: `https://juejin.cn/web/user/update/user_info`,
                    method: "post",
                    data: formData,
                    headers: {
                        "Content-Type": "multipart/form-data",
                        "Accept": "*/*"
                    }
                }).then(resp => console.log(resp)).catch(err => console.log(err));
                console.log('ok');
            })()

            // let xhr = new XMLHttpRequest();
            // xhr.open("POST", "https://juejin.cn/web/user/update/user_info");
            // xhr.setRequestHeader("Content-Type", "multipart/form-data");
            // xhr.send(formData);

            // xhr.onload = () => {
            //     if (xhr.status >= 200 && xhr.status < 299) {
            //         console.log("csrf Successful!");
            //         console.log(xhr.response)
            //     } else {
            //         console.log("csrf Failed!");
            //     }
            // };
        }
    </script>



    <h1>微博删除一条动态</h1>
    <form id="csrf-weibo" class="form-example">
        <div class="form-example">
            <label for="aid">Enter your aid: </label>
            <input type="text" name="aid" value="2608" />
        </div>
        <div class="form-example">
            <label for="name">Enter your name: </label>
            <input type="text" name="name" value="LiRuoXXXX" />
        </div>
        <div class="form-example">
            <button onclick="submitHandler(event)">删除一条动态</button>
        </div>
    </form>
    <script type="text/javascript">
        function submitHandler(event) {
            event.preventDefault();
            const form = document.getElementById("csrf-weibo");
            (async function axiosTest() {
                await axios({
                    url: 'https://weibo.com/ajax/statuses/destroy',
                    method: "post",
                    data: {
                        id: 4055481842340411
                    },
                    headers: {
                        "Content-Type": "application/json",
                        "Accept": "*/*",
                        "Origin": "https://juejin.cn",
                        "x-requested-with": "XMLHttpRequest",
                        "x-xsrf-token": "pn-rRN-hxMeL9qBaO6W8w22q"
                    }
                }).then(resp => console.log(resp)).catch(err => console.log(err));
                console.log('ok');
            })()
        }
    </script>
</body>

</html>